/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUILayer;
import javax.swing.table.AbstractTableModel;
import ControlLayer.*;
import java.util.ArrayList;

/**
 *
 * @author Thomas
 */
public class VaresalgTableModel extends AbstractTableModel
{
     
    private OrdreRegisterCtrl ordreRegisterCtrl;
    private VareKatalogCtrl vareKatalogCtrl;
    private boolean DEBUG = false;
    private String searchText;
    private String[] columnNames = {"Varenavn",
                                        "VareID",
                                        "Antal",
                                        "Stk pris",
                                        "Pris"};
    Object[][] data = {{"", 0, 0, 0, 0, 0}, 
        {"", 0, 0, 0, 0, 0},
        {"", 0, 0, 0, 0, 0},
        {"", 0, 0, 0, 0, 0},
        {"", 0, 0, 0, 0, 0},
        {"", 0, 0, 0, 0, 0}};
    


public VaresalgTableModel()
{
    vareKatalogCtrl = new VareKatalogCtrl();
}
    
    
    @Override
    public int getColumnCount() {
            return columnNames.length;
        }

    @Override
        public int getRowCount() {
            return data.length;
        }

    @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

    @Override
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
    @Override
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
    @Override
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 1) {
                return false;
            } else {
                return true;
            }
        }

        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
    @Override
        public void setValueAt(Object value, int row, int col) {
            if (DEBUG) {
                System.out.println("Setting value at " + row + "," + col
                                   + " to " + value
                                   + " (an instance of "
                                   + value.getClass() + ")");
            }

            data[row][col] = value;
            fireTableCellUpdated(row, col);

            if (DEBUG) {
                System.out.println("New value of data:");
                printDebugData();
            }
        }
    
        /**
         * Metode der returnerer et to-dimensionelt array
         * @return data [][] Det to-dimensionelle array, der udgør tabellen
         */
        public Object[][] getData()
        {
            return data;
        }
    
        /**
         * Metode der nulstiller tabellen (to-dimensionelt array)
         */
        public void clear()
        {
            for(int i = 0; i < data.length; i++)
            {
                for(int j = 0; j < 6; j++)  
                {
                    data[i][j] = null;
                }
            }
        }

        private void printDebugData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();

            for (int i=0; i < numRows; i++) {
                System.out.print("    row " + i + ":");
                for (int j=0; j < numCols; j++) {
                    System.out.print("  " + data[i][j]);
                }
                System.out.println();
            }
            System.out.println("--------------------------");
    
            
        }
        
        public void test1()
        {
            fireTableDataChanged();
        }
        
        public void test()
        {
            ArrayList<Integer> vareIder = new ArrayList<Integer>();
            
            for(int i = 0; i < data.length; i++)
            {
                if((Integer)data[i][1] != 0)
                {
                    vareIder.add((Integer)data[i][1]);
                }
            }
            
            for(int i = 0; i < vareIder.size(); i++)
            {
                    data[i][0] = vareKatalogCtrl.findVareBeskrivelseId(vareIder.get(i)).hentNavn();
                    data[i][1] = vareKatalogCtrl.findVareBeskrivelseId(vareIder.get(i)).hentVareId();
                    data[i][3] = vareKatalogCtrl.findVareBeskrivelseId(vareIder.get(i)).hentPris();
                    data[i][4] = (Integer)data[i][2] * (Double)data[i][3];
            }
            fireTableDataChanged();
        }
        
}
